package com.outfood.common.common.response;






import com.outfood.common.common.replay.ReplyCode;

import java.io.Serializable;

/**
 *
 *
 * @param <T>
 */
public class RestResponse<T> implements Serializable {

    private static final long serialVersionUID = -1621190234177299002L;
    private static final int STATUS_SUCCESS = 200;
    /**
     * 返回状态码
     */
    private Integer code;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 返回信息
     */
    private String message;

    public RestResponse(){

    }

    /**
     * 返回分页数据
     * @param code
     * @param msg
     */

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> RestResponse<T> toPageData(T data) {
        return new RestResponse<T>(ReplyCode.SYS.SUCCESS, data,"分页数据");
    }
    public RestResponse(Integer code, String msg) {
        this.code = code;
        this.message = msg;
        this.data = null;
    }

    public RestResponse(Integer code, T data) {
        this.code = code;
        this.message = null;
        this.data = data;
    }

    public RestResponse(T data) {
        this.code = ReplyCode.SYS.SUCCESS.getKey();
        this.data = data;
        this.message = null;
    }

    public RestResponse(Pair<Integer, String> pair) {
        this.code = pair.getKey();
        this.message = pair.getValue();
        this.data = null;
    }

    public RestResponse(Pair<Integer, String> pair, T data, String msg) {
        this.code = pair.getKey();
        this.message = msg;
        this.data = data;
    }
    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> RestResponse<T> success(T data) {

        return new RestResponse<T>( data);
    }

    public RestResponse(Pair<Integer, String> pair, String msg) {
        this.code = pair.getKey();
        this.message = msg;
        this.data = null;
    }

    public RestResponse(String msg, T data) {
        this.code = ReplyCode.SYS.SUCCESS.getKey();
        this.data = data;
        this.message = msg;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String msg) {
        this.message = msg;
    }


    public boolean isFail() {
        if (null == code) {
            return false;
        }
        return !code.equals(ReplyCode.SYS.SUCCESS.getKey());
    }

    public boolean isSuccess() {
        return !isFail();
    }

}
